package com.seadragon.app.dms.interceptors;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.seadragon.app.dms.model.Customer;
import com.seadragon.app.dms.model.DmsConstants;
import com.seadragon.app.dms.model.MenuItemData;

public class MenuHandlerInterceptor extends HandlerInterceptorAdapter {

	private List<MenuItemData> fullMenuList;
	
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView mav) throws Exception {
    		List<MenuItemData> menuList = new ArrayList<MenuItemData>();
    		Customer user = (Customer)request.getSession().getAttribute(DmsConstants.SESSION_SCOPED_USER);
    		for (MenuItemData item : this.fullMenuList) {
    			if (StringUtils.isEmpty(item.getRequiredRole())||(user!=null&&item.getRequiredRole().equals("customer"))){//todo: change it to getRequiredRole().equals(user.getType())
    				menuList.add(item);
    			} 
    		}
    		mav.addObject("menuList", menuList);
    }

	public List<MenuItemData> getFullMenuList() {
		return fullMenuList;
	}

	public void setFullMenuList(List<MenuItemData> fullMenuList) {
		this.fullMenuList = fullMenuList;
	}
}
